<?php

/**
 * Article form base class.
 *
 * @method Article getObject() Returns the current form's model object
 *
 * @package    myarticles
 * @subpackage form
 * @author     Apul Gupta<apulmca2k4@gmail.com>
 */
abstract class BaseArticleForm extends BaseFormPropel
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'                    => new sfWidgetFormInputHidden(),
      'title'                 => new sfWidgetFormInputText(),
      'url_slug'              => new sfWidgetFormInputText(),
      'meta_title'            => new sfWidgetFormInputText(),
      'meta_keyword'          => new sfWidgetFormInputText(),
      'meta_description'      => new sfWidgetFormInputText(),
      'user_id'               => new sfWidgetFormPropelChoice(array('model' => 'User', 'add_empty' => false)),
      'article_views'         => new sfWidgetFormInputText(),
      'total_rating'          => new sfWidgetFormInputText(),
      'total_user_rated'      => new sfWidgetFormInputText(),
      'content'               => new sfWidgetFormTextarea(),
      'author_speech'         => new sfWidgetFormInputText(),
      'article_summary'       => new sfWidgetFormInputText(),
      'word_count'            => new sfWidgetFormInputText(),
      'article_source'        => new sfWidgetFormInputText(),
      'created_at'            => new sfWidgetFormDateTime(),
      'updated_at'            => new sfWidgetFormDateTime(),
      'is_active'             => new sfWidgetFormInputCheckbox(),
      'article_category_list' => new sfWidgetFormPropelChoice(array('multiple' => true, 'model' => 'Category')),
    ));

    $this->setValidators(array(
      'id'                    => new sfValidatorPropelChoice(array('model' => 'Article', 'column' => 'id', 'required' => false)),
      'title'                 => new sfValidatorString(array('max_length' => 400)),
      'url_slug'              => new sfValidatorString(array('max_length' => 200)),
      'meta_title'            => new sfValidatorString(array('max_length' => 200)),
      'meta_keyword'          => new sfValidatorString(array('max_length' => 200)),
      'meta_description'      => new sfValidatorString(array('max_length' => 300)),
      'user_id'               => new sfValidatorPropelChoice(array('model' => 'User', 'column' => 'id')),
      'article_views'         => new sfValidatorInteger(array('min' => -2147483648, 'max' => 2147483647, 'required' => false)),
      'total_rating'          => new sfValidatorInteger(array('min' => -2147483648, 'max' => 2147483647, 'required' => false)),
      'total_user_rated'      => new sfValidatorInteger(array('min' => -2147483648, 'max' => 2147483647, 'required' => false)),
      'content'               => new sfValidatorString(),
      'author_speech'         => new sfValidatorString(array('max_length' => 1000, 'required' => false)),
      'article_summary'       => new sfValidatorString(array('max_length' => 1000, 'required' => false)),
      'word_count'            => new sfValidatorInteger(array('min' => -32768, 'max' => 32767, 'required' => false)),
      'article_source'        => new sfValidatorString(array('max_length' => 1000, 'required' => false)),
      'created_at'            => new sfValidatorDateTime(array('required' => false)),
      'updated_at'            => new sfValidatorDateTime(array('required' => false)),
      'is_active'             => new sfValidatorBoolean(array('required' => false)),
      'article_category_list' => new sfValidatorPropelChoice(array('multiple' => true, 'model' => 'Category', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('article[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    parent::setup();
  }

  public function getModelName()
  {
    return 'Article';
  }


  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['article_category_list']))
    {
      $values = array();
      foreach ($this->object->getArticleCategorys() as $obj)
      {
        $values[] = $obj->getCategoryId();
      }

      $this->setDefault('article_category_list', $values);
    }

  }

  protected function doSave($con = null)
  {
    parent::doSave($con);

    $this->saveArticleCategoryList($con);
  }

  public function saveArticleCategoryList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['article_category_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $c = new Criteria();
    $c->add(ArticleCategoryPeer::ARTICLE_ID, $this->object->getPrimaryKey());
    ArticleCategoryPeer::doDelete($c, $con);

    $values = $this->getValue('article_category_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new ArticleCategory();
        $obj->setArticleId($this->object->getPrimaryKey());
        $obj->setCategoryId($value);
        $obj->save();
      }
    }
  }

}
